#include "MainWindow.h"
#include <QResizeEvent>
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QList>
#include <QStringList>
#include <QFileDialog>
#include <qdebug.h>
#include <QGraphicsItem>
#include <QBrush>

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{
        resize(800,600);
        setWindowTitle("Painter");
        setFixedSize(800,600);
        graphicsView = new QGraphicsView();
        graphicsView->resize(500, 450);
        QVBoxLayout *vlayout = new QVBoxLayout;
        vlayout->addWidget(graphicsView);
        setLayout(vlayout);
        scene = new QGraphicsScene();
        scene->setSceneRect(50,80,700,450);
        graphicsView->setScene(scene);
        intensivnost=100;
        tmr = new QTimer();
        trd = new tred();
        tmr->setInterval(1000);
        connect(tmr, &QTimer::timeout, this, &MainWindow::generationFigure);
        connect(trd,&tred::retFig,this,&MainWindow::prinf);
        tmr->start(1000);
}

void MainWindow::prinf(QGraphicsItem *itm)
{
    scene->addItem(itm);
}


void MainWindow::saveFile()
{
    QPixmap pixmap = graphicsView->grab();
    pixmap.save("generationImage.jpg");
}



void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if (event->key()==Qt::Key_B)
    {
        if(intensivnost<250)
        {
            intensivnost+=10;
            QList <QGraphicsItem* > lst  =scene->items();
                    for(int i=0;i<lst.size();i++)
                        {
                           if (lst[i]->data(0)=='e')
                           {
                            QGraphicsEllipseItem *items=(QGraphicsEllipseItem*)(lst[i]);
                            items->setBrush(QBrush(QColor(255,0,255,intensivnost)));
                           }
                           else
                           {
                             QGraphicsRectItem *items=(QGraphicsRectItem*)(lst[i]);
                             items->setBrush(QBrush(QColor(255,0,255,intensivnost)));
                           }
                        }
        }
    }
    else if (event->key()==Qt::Key_M)
    {
        if(intensivnost>10)
        {
            intensivnost-=10;
            QList <QGraphicsItem* > lst  =scene->items();
            for(int i=0;i<lst.size();i++)
                {
                   if (lst[i]->data(0)=='e')
                   {
                    QGraphicsEllipseItem *items=(QGraphicsEllipseItem*)(lst[i]);
                    items->setBrush(QBrush(QColor(255,0,255,intensivnost)));
                   }
                   else
                   {
                     QGraphicsRectItem *items=(QGraphicsRectItem*)(lst[i]);
                     items->setBrush(QBrush(QColor(255,0,255,intensivnost)));
                   }
                }
        }
    }
    else if (event->key()==Qt::Key_Space)
    {
        tmr->stop();
        msgBox.setWindowTitle("Savings");
        msgBox.setIcon(QMessageBox::Question);
        msgBox.setText("Do you want to save?");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox.setDefaultButton(QMessageBox::Yes);
        int ret = msgBox.exec();
        switch (ret)
        {
            case QMessageBox::Yes:
               {
                saveFile();
                break;
               }
        }
        tmr->start();
    }
}

void MainWindow::generationFigure()
{
    trd->start();
}

MainWindow::~MainWindow()
{
    delete tmr;
    delete scene;
    delete trd;
    delete graphicsView;
}
